﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace StringUtilHelp
{
    public sealed class StringToolV2
    {
        /// <summary>
        /// 字符串逆转
        /// </summary>
        /// <param name="str">需要逆转的字符串</param>
        /// <returns>逆转后的字符串</returns>
        public static string StringReverse(string str)
        {
            try
            {
                if (string.IsNullOrEmpty(str))
                    throw new ArgumentNullException("str");
                char[] _chars = str.ToCharArray();
                Array.Reverse(_chars);
                return new string(_chars);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("StringReverse Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// Byte[0]=秒，Byte[1]=分，Byte[2]=时，Byte[3]=日，Byte[4]=月，Byte[5,6]=年
        /// 为秒、分、时、日、月、年的BCD码
        /// 如：22 21 20 18 11 12 20=2012年11月12日20时21分22秒
        /// </summary>
        /// <param name="dateTime">表示时间的BCD码</param>
        /// <returns>如：22 21 20 18 11 12 20=2012年11月12日20时21分22秒</returns>
        public static string FormatDateTimeBCD(string dateTime)
        {

            try
            {
                dateTime = dateTime.Trim();
                if (string.IsNullOrEmpty(dateTime))
                    throw new ArgumentNullException("dateTime IsNullOrEmpty");
                if (dateTime.Length != 14)
                    throw new ArgumentException("dateTime");
                string _second = dateTime.Substring(0, 2);//秒
                string _minute = dateTime.Substring(2, 2);//分
                string _hour = dateTime.Substring(4, 2);//小时
                string _day = dateTime.Substring(6, 2);//天
                string _month = dateTime.Substring(8, 2);//月
                string _yearPart1 = dateTime.Substring(10, 2);//年
                string _yearPart2 = dateTime.Substring(12, 2);//年
                return string.Format("{0}{1}-{2}-{3} {4}:{5}:{6}", _yearPart2, _yearPart1, _month, _day, _hour, _minute, _second);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("FormatDateTimeBCD Exception:{0}", ex.Message.Trim()));
            }
        }

        public static string DateTimeBCDFormart(DateTime dateTime)
        {
            try
            {
                string _second = dateTime.Second.ToString().PadLeft(2, '0');
                string _minute = dateTime.Minute.ToString().PadLeft(2, '0');
                string _hour = dateTime.Hour.ToString().PadLeft(2, '0');
                string _day = dateTime.Day.ToString().PadLeft(2, '0');
                string _month = dateTime.Month.ToString().PadLeft(2, '0');
                string _yearFull = dateTime.Year.ToString();
                string _yearRev = string.Format("{0}{1}", _yearFull.Substring(2, 2), _yearFull.Substring(0, 2));
                return string.Format("{0}{1}{2}{3}{4}{5}", _second, _minute, _hour, _day, _month, _yearRev);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("DateTimeBCDFormart Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// 四位长度的字符串高地位呼唤
        /// eg:1122 ==>2211
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ExchangeHightToLow(string str)
        {
            try
            {
                str = str.Trim();
                if (string.IsNullOrEmpty(str))
                    throw new ArgumentNullException("str IsNullOrEmpty");
                if (str.Length != 4)
                    throw new ArgumentException("str");
                return string.Format("{0}{1}", str.Substring(2, 2), str.Substring(0, 2));
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ExchangeHightToLow Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// 清空StringBuilder
        /// </summary>
        /// <param name="sb"></param>
        public static void ClearSBuilder(StringBuilder sb)
        {
            try
            {
                if (sb != null)
                {
                    sb.Length = 0;
                    sb.Capacity = 0;

                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ClearStringBuilder Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// 检查StringBuilder是否为null，当为null的时候，实例化返回。
        /// </summary>
        /// <param name="sb"></param>
        /// <returns></returns>
        public static StringBuilder CheckSBuilderNullInit(StringBuilder sb)
        {
            try
            {
                if (sb == null)
                    sb = new StringBuilder();
                return sb;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("CheckSBuilderNullInit Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// 检查decimal 在符号.之前有多少长度
        /// eg:12.121表示两个长度
        /// </summary>
        /// <param name="decimalStr">需要检查的decimal</param>
        /// <param name="val">返回的decimal</param>
        /// <param name="decimalLen"> 至少多少长度eg:12.121表示两个长度</param>
        /// <returns></returns>
        public static bool MaxThanDecimal(string decimalStr, out decimal val, int decimalLen)
        {
            if (!decimal.TryParse(decimalStr, out val))
                return false;
            int _separatorIndex = decimalStr.LastIndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator);
            return ((_separatorIndex != -1 && _separatorIndex <= decimalLen) || (_separatorIndex == -1 && decimalStr.Length <= decimalLen));
        }
        public static string ClearBlank(string str)
        {
            if (!string.IsNullOrEmpty(str))
                str = str.Replace(" ", "");
            return str;
        }
    }
}
